﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DA;
using Entities;
using ERP.Membership;

namespace ERP.UserControl
{
    public partial class OrdersReport : System.Web.UI.UserControl
    {
        private IEnumerable<ERP_Orders> _dataSourceU = null;

        public IEnumerable<ERP_Orders> DataSourceU
        {
            set
            {
                _dataSourceU = value;
                RepOrders.DataSource = value;
            }
            get
            {
                return _dataSourceU;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cmbStatus.DataSource = Enum.GetNames(typeof(OrderStatus));
                cmbStatus.SelectedIndex = 0;
                cmbStatus.DataBind();

                IEnumerable<ERP_Orders> filteredOrders = DataSourceU;

                filteredOrders = FilterByStatus(OrderStatus.ממתין_לאישור_מנהל);

                RepOrders.DataSource = filteredOrders.ToList();
                RepOrders.DataBind();
            }
        }

        public void btnFilter_onClick(object sender, EventArgs e)
        {
            IEnumerable<ERP_Orders> filteredOrders = DataSourceU;
            if(cmbStatus.SelectedValue == OrderStatus.ממתין_לאישור_מנהל.ToString())
                filteredOrders = FilterByStatus(OrderStatus.ממתין_לאישור_מנהל);
            if (cmbStatus.SelectedValue == OrderStatus.במלאי.ToString())
                filteredOrders = FilterByStatus(OrderStatus.במלאי);
            if (cmbStatus.SelectedValue == OrderStatus.טיוטא.ToString())
                filteredOrders = FilterByStatus(OrderStatus.טיוטא);
            if (cmbStatus.SelectedValue == OrderStatus.מאושר.ToString())
                filteredOrders = FilterByStatus(OrderStatus.מאושר);
            if (cmbStatus.SelectedValue == OrderStatus.נדחה.ToString())
                filteredOrders = FilterByStatus(Enum.Parse(typeof(OrderStatus,cmbStatus.SelectedValue.ToString()), cmbStatus.SelectedValue.ToString());

            RepOrders.DataSource = filteredOrders.ToList();
            RepOrders.DataBind();

            /*
             HideError();
            

            if (rdoPriceExacly.Checked)
            {
                decimal? exactPrice = txtExactPrice.Text.ToDecimal();
                if (exactPrice.HasValue)
                    filteredOrders = filteredOrders.Where(x => x.Price == exactPrice);
                else
                    ShowError("The price value is not a valid number.");
            }
            else if (rdoPriceRange.Checked)
            {
                decimal? PriceRangeMin = txtPriceRangeMin.Text.ToDecimal();
                decimal? PriceRangeMax = txtPriceRangeMax.Text.ToDecimal();
                if (PriceRangeMin.HasValue && PriceRangeMax.HasValue)
                    filteredOrders = filteredOrders.Where(x => x.Price >= PriceRangeMin && x.Price <= PriceRangeMax);
                else
                    ShowError("The price value is not a valid number.");
            }

            if (rdoDateExact.Checked)
            {
                DateTime exactDate;

                if (DateTime.TryParse(txtExactDate.Text, out exactDate))
                {
                    exactDate = new DateTime(exactDate.Year, exactDate.Month, exactDate.Day);
                    filteredOrders = filteredOrders.Where(x => x.CreationDate >= exactDate && x.CreationDate <= exactDate.AddDays(1));
                }
                else
                    ShowError("The Date value is not valid.");
            }
            else if (rdoDateRange.Checked)
            {
                DateTime minDate;
                DateTime maxDate;

                if (
                    DateTime.TryParse(txtDateRangeMin.Text, out minDate) &&
                    DateTime.TryParse(txtDateRangeMax.Text, out maxDate)
                    )
                {
                    filteredOrders = filteredOrders.Where(x => x.CreationDate >= minDate && x.CreationDate <= maxDate);
                }
                else
                    ShowError("The Date value is not valid.");
            }

           */

        }

        public IEnumerable<ERP_Orders> FilterByStatus(OrderStatus status)
        {
            int statusI = (int)status;
            IEnumerable<ERP_Orders> filteredOrders = DataSourceU;
            return filteredOrders = filteredOrders.Where(x => x.OrderStatus == statusI);
            
        }

        private void ShowError(string message)
        {
            this.lblMessage.Text = message;
            this.lblMessage.Visible = true;
        }

        protected void RepOrders_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.DataItem == null)
                return;

            ERPMembership erpm = new ERP.Membership.ERPMembership();
            UserData ud = erpm.GetCurrentLoggedInUser();
            ERP_Orders order = (ERP_Orders)e.Item.DataItem;

            Control OrderIdWithLinkContainer = e.Item.FindControl("OrderIdWithLinkContainer");
            Control OrderIdContainer = e.Item.FindControl("OrderIdContainer");
            Control printLink = e.Item.FindControl("printLink");
            Control btnStatusUpdate = e.Item.FindControl("btnStatusUpdate");
            Control lblStatusOnly = e.Item.FindControl("lblStatusOnly");


            if (order.UserId == ud.UserId)
            {
                if (order.OrderStatus == (int)OrderStatus.טיוטא || order.OrderStatus == (int)OrderStatus.ממתין_לאישור_מנהל)
                    OrderIdWithLinkContainer.Visible = true;
            }
            else
                OrderIdContainer.Visible = true;

            if (ud.IsUserAllowed(Permission.OrdersApprover))
                btnStatusUpdate.Visible = true;

            if (ud.IsUserAllowed(Permission.OrdersWrite))
                lblStatusOnly.Visible = true;

            if (order.OrderStatus == (int)OrderStatus.מאושר && ud.IsUserAllowed(Permission.OrdersWrite) == true)
            {
                OrderIdContainer.Visible = true;
                printLink.Visible = true;
            }
             
        }
            
        private void HideError()
        {
            this.lblMessage.Text = string.Empty;
            this.lblMessage.Visible = false;
        }
    }
}